package org.example.word;

import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;

import static org.example.word.CreateWordXWPFHyperlinkRun.createHyperlinkRun;

public class ReadWordDemo1 {
    public static final String FILE_PATH = "E:\\work_template\\代办通知单.docx";
    public static void main(String[] args) {
        StringBuffer text = new StringBuffer();
        File workFile = new File(FILE_PATH);
        try (XWPFDocument doc = new XWPFDocument(Files.newInputStream(Paths.get(FILE_PATH)))) {
            // First up, all our paragraph based text
            List<XWPFTable> tables = doc.getTables();
            for (XWPFTable table : tables) {
                int rowCount = table.getNumberOfRows();

                for (int i = 0; i < rowCount; i++) {
                    XWPFTableRow row = table.getRow(i);

                    String flag = row.getCell(0).getText();

                    if (i == 13) {
                        XWPFParagraph paragraph = doc.createParagraph();

                        XWPFRun run = paragraph.createRun();
                        run.setText("This is a text paragraph having ");

                        XWPFHyperlinkRun hyperlinkrun = createHyperlinkRun(paragraph, "https://www.google.de");
                        hyperlinkrun.setText("a link to Google");
                        hyperlinkrun.setColor("0000FF");
                        hyperlinkrun.setUnderline(UnderlinePatterns.SINGLE);

                        run = paragraph.createRun();
                        run.setText(" in it.");

                        paragraph.addRun(run);
                        paragraph.addRun(hyperlinkrun);
                        paragraph.addRun(run);
                        row.getCell(1).setParagraph(paragraph);
                        /*System.err.println("第" + i + "行");
                        int size = 0;
                        if (row.getTableCells().size() >= 2) {
                            String cellText = row.getCell(1).getText();

                            cellText = cellText.replaceAll("[\\s\\u00A0]+","");
                            if (cellText != null && cellText.contains("点击下载")) {
                                cellText = cellText.replace("点击下载",",");
                                for (String s : cellText.split(",")) {
                                    //s = s.replaceAll("[\\s\\u00A0]+","").trim();
                                    System.err.println("s=" + s);
                                    size++;
                                }

                            }
                            if (row.getCell(1).getParagraphs() != null) {
                                for (XWPFParagraph paragraph : row.getCell(1).getParagraphs()) {
                                    for (XWPFRun run : paragraph.getRuns()) {
                                        for (CTText ctText : run.getCTR().getInstrTextArray()) {
                                            System.err.println(ctText.getStringValue().replace("HYPERLINK \"","").replace("\"","").trim());
                                        }
                                    }
                                }
                            }

                        }*/
                    }


                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
